﻿using System;
using System.Linq;
using System.ServiceModel.Description;

namespace YawetagLib.ServiceModel
{
    public class ServiceHost : System.ServiceModel.ServiceHost
    {
        protected ServiceHost()
        {
        }

        public ServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        public ServiceHost(object singletonInstance, params Uri[] baseAddresses)
            : base(singletonInstance, baseAddresses)
        {
        }

        protected TBehavior EnsureServiceBehavior<TBehavior>() where TBehavior : class, IServiceBehavior, new()
        {
            TBehavior behavior = Description.Behaviors.OfType<TBehavior>().SingleOrDefault();
            if (behavior == null)
            {
                behavior = new TBehavior();
                Description.Behaviors.Add(behavior);
            }

            return behavior;
        }
    }
}
